<template>
  <common-list>
    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        @submit.native.prevent
        v-show="showSearch"
        label-width="65px"
      >
        <el-form-item label="业务系统" prop="applicationSystem">
          <el-select
            v-model="queryParams.applicationSystem"
            class="custom-select"
          >
            <el-option label="全部" :value="null"></el-option>
            <el-option
              class="custom-select"
              v-for="(item, index) in businessCodeList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictLabel"
            >
              <span class="selectList">
                <common-marquee-text :content="item.dictLabel">
                </common-marquee-text>
              </span>
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="库名/用户名" prop="dbAccount">
          <common-input
            v-model="queryParams.dbAccount"
            clearable
            @enter="handleQuery"
          />
        </el-form-item> -->
        <!-- <el-form-item label="数据库类型" prop="dbType">
          <el-select
            v-model="queryParams.dbType"
            style="width: 100%"
            filterable
          >
            <el-option label="全部" :value="null"></el-option>
            <el-option
              v-for="item in dbTypeList"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="数据库名称" prop="dbName">
          <common-input
            v-model="queryParams.dbName"
            clearable
            @enter="handleQuery"
          />
        </el-form-item> -->

        <el-form-item label="表名" prop="tableName">
          <common-input
            v-model="queryParams.tableName"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>
        <!-- <el-form-item label="主机IP" prop="ip">
          <common-input
            v-model="queryParams.ip"
            clearable
            @enter="handleQuery"
          />
        </el-form-item> -->
        <el-form-item label="敏感级别" prop="level">
          <el-select v-model="queryParams.level" filterable>
            <el-option label="全部" :value="null"></el-option>
            <el-option
              v-for="item in levelList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="数据分类" prop="typeName">
          <el-cascader
            class="custom-cascader-style"
            v-model="queryParams.typeName"
            :options="dataTypeList"
            :props="cascaderProps"
            clearable
          >
            <template slot-scope="{ node, data }">
              <common-marquee-text
                :content="data.typeName"
              ></common-marquee-text>
            </template>
          </el-cascader>
        </el-form-item>
        <el-form-item label="识别规则" prop="sensLabel">
          <el-select v-model="queryParams.sensLabel" filterable>
            <el-option label="全部" :value="null"></el-option>
            <el-option
              v-for="item in ruleList"
              :key="item.id"
              :label="item.ruleName"
              :value="item.ruleName"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="重要数据" prop="tagLabel">
          <el-select v-model="queryParams.tagLabel" filterable>
            <el-option label="全部" :value="null"></el-option>
            <el-option
              v-for="item in allTagsList"
              :key="item.tagId"
              :label="item.tagName"
              :value="item.tagName"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="核心数据" prop="tagLabel">
          <el-select v-model="queryParams.tagLabel" filterable>
            <el-option label="全部" :value="null"></el-option>
            <el-option
              v-for="item in allTagsList"
              :key="item.tagId"
              :label="item.tagName"
              :value="item.tagName"
            />
          </el-select>
        </el-form-item> -->
      </el-form>
    </common-search>
    <el-row slot="tools" :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-switch
          v-model="isMatchNums"
          @change="matchNumChange"
          active-color="#13ce66"
          inactive-color="#909399"
          active-text="仅含有敏感字段数据"
          inactive-text="所有数据"
        >
        </el-switch>
      </el-col>
    </el-row>
    <div slot="tools" :gutter="10" class="mb8" style="margin-top: 15px">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-upload2"
          :loading="exportLoading"
          @click="handleExport"
          >导出
        </el-button>
      </el-col>
    </div>
    <common-table slot="content" v-loading="loading" :data="list">
      <el-table-column
        label="业务系统"
        align="left"
        prop="applicationSystem"
        width="160px"
        fixed
        show-overflow-tooltip
      />
      <el-table-column
        label="数据库名称"
        align="left"
        prop="dbName"
        width="160px"
        fixed
        show-overflow-tooltip
      />
      <el-table-column
        label="主机IP"
        align="left"
        show-overflow-tooltip
        prop="ip"
        width="160px"
      />
      <el-table-column
        label="数据库类型"
        align="left"
        prop="dbType"
        width="100px"
      />
      <el-table-column
        label="库名/用户名"
        align="left"
        prop="dbAccount"
        width="100px"
        show-overflow-tooltip
      />
      <el-table-column
        label="表名"
        align="left"
        prop="tableName"
        show-overflow-tooltip
      />
      <el-table-column
        label="表行数"
        align="left"
        prop="rowNum"
        show-overflow-tooltip
      />
      <el-table-column
        label="表容量(单位/M)"
        align="left"
        prop="tableCapacity"
        show-overflow-tooltip
      />
      <el-table-column
        label="数据分类"
        align="left"
        prop="typeName"
        show-overflow-tooltip
      />
      <el-table-column label="敏感级别" prop="levelName" align="left" />
      <el-table-column
        label="识别规则"
        align="left"
        prop="sensLabel"
        show-overflow-tooltip
      />

      <el-table-column
        label="重要数据"
        align="left"
        show-overflow-tooltip
        prop="importantData"
      />

      <el-table-column
        label="核心数据"
        align="left"
        show-overflow-tooltip
        prop="coreData"
      />
      <el-table-column
        label="扫描时间"
        align="left"
        fixed="right"
        prop="updateTime"
        width="170px"
      />
    </common-table>

    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </common-list>
</template>

<script>
import {
  level,
  queryTableListByPage,
  queryTagList,
  queryDataTypeTreeList,
  queryAllTagList,
} from "@/api/datascan/scanresults/table";
import { queryListByTypeCode } from "@/api/datascan/scanresults/column";
import { formatDate, deepClone } from "@/utils";

import BaseList from "@/mixins/BaseList";
import { mapGetters } from "vuex";
import { queryList } from "@/api/datahandler/discernRule";
import { listLevel } from "@/api/datascan/level/level";
import { queryBusinessList } from "@/api/datascan/dbinfo/info";
export default {
  name: "Table",
  mixins: [BaseList],
  props: {
    dataId: {
      type: Number,
    },
  },
  components: {},
  data() {
    return {
      businessCodeList: [],
      exportLoading: false,
      historyLoading: false,
      // 遮罩层
      loading: true,
      // 显示查询条件
      showSearch: true,
      // 总条数
      total: 0,
      // 的基本信息表格数据
      list: [],
      levelList: [],
      dataTypeList: [],
      classifyEditShow: false,
      dbTypeList: [
        {
          label: "oracle",
          value: "oracle",
        },
        {
          label: "mysql",
          value: "mysql",
        },
        {
          label: "teledb",
          value: "teledb",
        },
        {
          label: "pgsql",
          value: "pgsql",
        },
      ], // 数据库类型
      cascaderProps: {
        label: "typeName",
        value: "typeName",
        emitPath: false,
      },
      isMatchNums: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sensLabel: null,
        matchNums: 0, // 是否包含敏感字符  所有数据时为空字符串 仅包含敏感词时为 0
        dbId: null,
        dbType: null,
        dbName: null,
        tableName: null,
        level: null,
        typeName: null,
        tagLabel: null,

        typeCode: null,
        ip: null,
        deptIds: null,
        dbAccount: null,
      },
      matchNums: null,
      info: {
        dbType: null,
        dbName: null,
        tableName: null,
        levelName: null,
        sensLabel: null,
      },
      mappers: [
        {
          label: "主机Ip",
          key: "ip",
          width: "50%",
        },
        {
          label: "数据库类型",
          key: "dbType",
          width: "50%",
        },
        {
          label: "库名/用户名",
          key: "dbAccount",
          width: "50%",
        },
        {
          label: "数据库名称",
          key: "dbName",
          width: "50%",
        },
        {
          label: "表名",
          key: "tableName",
          width: "50%",
        },
        {
          label: "敏感级别",
          key: "levelName",
          width: "50%",
        },
        {
          label: "数据分类",
          width: "100%",
          key: "typeNames",
        },
        {
          label: "识别规则",
          width: "100%",
          key: "sensLabel",
        },
        {
          label: "重要数据",
          width: "100%",
          key: "importantData",
        },
        {
          label: "核心数据",
          width: "100%",
          key: "coreData",
        },
        {
          label: "业务系统",
          key: "applicationSystem",
          width: "50%",
        },
        // {
        //   label: "数据分类代码",
        //   key: "typeCode",
        // },
        // {
        //   label: '标签名称',
        //   key: 'tagName'
        // },
        {
          label: "扫描时间",
          width: "100%",
          key: "updateTime",
        },
      ],
      infoShow: false,

      // --- 列
      columnShow: false,
      columnList: [],
      isColumnMatch: false,
      columnTotal: 0,
      columnQueryParams: {
        pageNum: 1,
        pageSize: 10,
        dbId: null,
        tableName: null,
        matchNums: "",
      },

      tagsList: [], // 所有标签
      allTagsList: [],

      // 标签
      tagsShow: false,
      tableTagsList: [], // 当前数据选择的标签
      tagsForm: {
        tagIdList: [],
        dbId: null,
        tableName: null,
      },
      tagsQueryParams: {
        dbId: null,
        tableName: null,
      },
      tagLoading: false,
      currentParent: 4,
      singleDeptList: [],
      currentCheckedIds: [],
      dbId: "",
      tagIds: [],

      itemInfo: null,
      // --------重构设置重要数据----------
      showImportData: false,
      showCoreData: false,
      showDiserRule: false,
      showDataType: false,
      ruleList: [],
    };
  },

  created() {
    this.getList();
    this.getDataTypeList();
    this.getTreeList();
    this.getTagsList();
    this.getDataSourceTypeList();
    this.getAllTagList();
    this.getRuleList();
    this.queryBusinessCodeList();
  },
  computed: {
    ...mapGetters(["systemInfo"]),
    getName() {
      return (val) => {
        let name = "",
          index = this.levelList.findIndex((f) => f.value === val);
        if (index > -1) {
          name = this.levelList[index].label;
        }
        return name;
      };
    },
    getTypeName() {
      return (val) => {
        let name = [],
          ids = val ? val.split(",").map(Number) : [];
        const getTypeName = (data) => {
          if (data.length > 0) {
            data.forEach((f) => {
              if (ids.includes(f.typeId) && !name.includes(f.typeName)) {
                name.push(f.typeName);
              } else if (f.children && f.children.length) {
                getTypeName(f.children);
              }
            });
          }
        };
        getTypeName(this.dataTypeList);
        return name.join(",");
      };
    },
  },
  methods: {
    queryBusinessCodeList() {
      queryBusinessList().then((res) => {
        if (res && res.code === 200) {
          this.businessCodeList = res.data;
        } else {
          this.businessCodeList = [];
        }
      });
    },
    // 获取识别规则
    getRuleList() {
      queryList({ status: 0 }).then((res) => {
        if (res && res.code === 200) {
          this.ruleList = res.rows;
        } else {
          this.dataList = [];
        }
      });
    },

    matchNumChange() {
      this.queryParams.matchNums = this.isMatchNums ? 0 : "";
      this.handleQuery();
    },
    handleExport() {
      this.exportLoading = true;
      this.download(
        "datascan/b/scanresulttable/exportTemplate ",
        {
          ...this.queryParams,
          pageSize: 10000,
        },
        `扫描结果_表_${new Date().getTime()}.xlsx`,
        1800000
      ).finally(() => {
        this.exportLoading = false;
      });
    },

    /** 查询的基本信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.dbId = this.dataId;
      queryTableListByPage(this.queryParams)
        .then((res) => {
          if (res && res.code === 200) {
            this.list = res.data.dataList.reduce((r, c) => {
              c.updateTime = formatDate(c.updateTime);
              c.sensLabelList = c.sensLabel ? c.sensLabel.split(",") : [];
              r.push(c);
              return r;
            }, []);
            this.total = res.data.totalCount;
          } else {
            this.list = [];
            this.total = 0;
            this.handleListTips(res);
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    getTagsList() {
      queryTagList().then((res) => {
        if (res && res.code === 200) {
          this.tagsList = res.data.dataList;
        } else {
          this.tagsList = [];
        }
      });
    },
    getAllTagList() {
      queryAllTagList().then((res) => {
        if (res && res.code === 200) {
          this.allTagsList = res.data.dataList;
        } else {
          this.allTagsList = [];
        }
      });
    },
    /** 查询按钮操作 */
    handleQuery() {
      this.queryParams.matchNums = this.isMatchNums ? "0" : "";
      this.queryParams.pageNum = 1;
      this.queryParams.ip =
        this.queryParams.ip === "" ? null : this.queryParams.ip;
      this.queryParams.dbName =
        this.queryParams.dbName === "" ? null : this.queryParams.dbName;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.isMatchNums = true;
      this.queryParams = this.$options.data().queryParams;
    },

    submitForm() {},

    getDataTypeList() {
      queryDataTypeTreeList({}).then((res) => {
        if (res && res.code === 200) {
          let dataTypeList = res.data.dataList;
          /*  dataTypeList.unshift({
                typeName: '全部',
                typeId: null
              })*/
          this.dataTypeList = dataTypeList;
        } else {
          this.dataTypeList = [];
        }
      });
    },
    getTreeList() {
      listLevel().then((res) => {
        if (res && res.code === 200) {
          this.levelList = res.data.dataList.reduce((r, c) => {
            r.push({
              label: c.name,
              value: c.levelId,
            });
            return r;
          }, []);
        } else {
          this.levelList = [];
        }
      });
    },
    getDataSourceTypeList() {
      queryListByTypeCode().then((res) => {
        if (res && res.code === 200) {
          this.dbTypeList = res.data.dataList.reduce((r, c) => {
            r.push({
              label: c.name,
              value: c.value,
            });
            return r;
          }, []);
        } else {
          this.dbTypeList = [];
        }
      });
    },
  },
  mounted() {},
};
</script>

<style lang="scss" scoped>
.info-dialog {
  ::v-deep .el-dialog__footer {
    border-top: 1px solid #eee;
  }
}

.table {
  padding: 0 12px 12px 12px;
}

.el-dropdown-link {
  color: var(--theme-color);
}
</style>
